//
//  ExampleVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "ExampleVC.h"
#import "ExampleCollectionViewCell.h"
#import "RecommendModel.h"
#import "ListModel.h"
#import "ExampleModel.h"
#import "RecommendCollectionReusableView.h"
#import "JGRankingVC.h"

@interface ExampleVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *backCollectionV;/**< 底层collectionView */
@property (nonatomic, strong) NSMutableArray *sectionModelArr;/**< 2个分区的数组 */


@end

@implementation ExampleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.sectionModelArr = [NSMutableArray array];
    
//    [self dataHander];
    [self createView];
    [self addHeader];
    
}
#pragma mark - 创建视图
- (void)createView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 80);
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    
    self.backCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT - 153) collectionViewLayout:layout];
    [self.view addSubview:_backCollectionV];
    self.backCollectionV.delegate = self;
    self.backCollectionV.dataSource = self;
    self.backCollectionV.backgroundColor = [UIColor whiteColor];
    
    // 注册cell和头视图
    [self.backCollectionV registerClass:[ExampleCollectionViewCell class] forCellWithReuseIdentifier:@"ExampleCollectionViewCell"];
    [self.backCollectionV registerClass:[RecommendCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RecommendCollectionReusableView"];
    
}
#pragma mark - 协议方法
// 返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.sectionModelArr.count;
}
// 返回cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    RecommendModel *model = [[RecommendModel alloc] init];
    model = self.sectionModelArr[section];
    
    return model.list.count;
}
// 返回cell样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ExampleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ExampleCollectionViewCell" forIndexPath:indexPath];
    
    RecommendModel *model = [[RecommendModel alloc] init];
    model = self.sectionModelArr[indexPath.section];
    
    ListModel *model2 = model.list[indexPath.row];
    cell.model = model2;
    
    return cell;
}
// 返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        RecommendCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"RecommendCollectionReusableView" forIndexPath:indexPath];
        RecommendModel *model = [[RecommendModel alloc] init];
        model = self.sectionModelArr[indexPath.section];
        
        [header setTitle:model.title];
        
        return header;
    }
    
    return nil;
}
// cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    JGRankingVC *rankingVC = [[JGRankingVC alloc] init];
    RecommendModel *model = [[RecommendModel alloc] init];
    model = self.sectionModelArr[indexPath.section];
    
    ListModel *model2 = model.list[indexPath.row];
    
    rankingVC.key = model2.key;
    rankingVC.contentType = model2.contentType;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            rankingVC.isKind = 0;
        }else{
            rankingVC.isKind = 1;
        }
    }else{
        rankingVC.isKind = 2;
    }
    
    [self.navigationController pushViewController:rankingVC animated:YES];
}

#pragma mark - 数据解析
- (void)dataHander{
    NSString *path = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26";
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"datas"];
        
        self.sectionModelArr = [RecommendModel modelHandleWithArray:arr];
        
        [_backCollectionV reloadData];
        [_backCollectionV.mj_header endRefreshing];
        
    } filed:^(NSError *error) {
        
        NSLog(@"解析失败");
        
    }];
}
/**< 下拉刷新 */
- (void)addHeader{
    self.backCollectionV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self dataHander];
        
    }];
    [self.backCollectionV.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
